package com.loongtech.bi.action.authManagement;

import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Author: yuri
 * Description: 角色管理
 * Date: 14:55 2017/5/3
 */
@RestController
@Scope("prototype")
@RequestMapping("/role")
public class SysRoleAction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SysRoleManager sysRoleManager;
    @Resource
    private SysUserProjectRoleManager sysUserProjectRoleManager;
    @Resource
    private SysRoleFunctionManager sysRoleFunctionManager;


    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> list() {
        List<EntitySysRole> list;
        try {
            list = sysRoleManager.findByQuery("from EntitySysRole order by id asc");
        } catch (Exception ex) {
            ex.printStackTrace();
            return  ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(),list);
    }

    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> add(EntitySysRole role) {
        // 判断角色名称是否已经被注册
        try {
            if (null != sysRoleManager.getBy(EntitySysRole.K.name, role.getName())) {
                return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
            }
            if (null != sysRoleManager.getBy("id", role.getId())) {
                return new ResultData<>(RetCode.eIdExist.getErrorcode(), RetCode.eIdExist.getErrorMsg());
            }
            // 注册
            sysRoleManager.persist(role);
            sysLogManager.addLog(session.getUserName(), "增加角色成功:" + role.getName());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(EntitySysRole role) {
        try {
            EntitySysRole cur = sysRoleManager.get(role.getId());
            if (cur != null) {
                sysRoleManager.merge(role);
                sysLogManager.addLog(session.getUserName(), "角色信息修改成功!roleId=" + role.getId());
                return ResultData.success();
            } else {
                sysLogManager.addLog(session.getUserName(), "角色信息修改失败,不存在角色roleId==" + role.getId());
                return new ResultData<>(RetCode.eModuleEmpty.getErrorcode(), RetCode.eModuleEmpty.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "delete.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delete(@RequestParam(value = "id") Integer roleId) {
        try {
            List<EntitySysUserRole> list1 = sysUserProjectRoleManager.findAll(EntitySysUserRole.K.roleId, roleId);
            if (null != list1 && !list1.isEmpty()) {
                return new ResultData<>(RetCode.eRelationExist.getErrorcode(), RetCode.eRelationExist.getErrorMsg());
            }
            List<EntitySysRoleFunction> list2 = sysRoleFunctionManager.findAll(EntitySysRoleFunction.K.roleId, roleId);
            if (null != list2 && !list2.isEmpty()) {
                return new ResultData<>(RetCode.eRelationExist.getErrorcode(), RetCode.eRelationExist.getErrorMsg());
            }
            if (null != sysRoleManager.get(roleId)) {
                sysRoleManager.removeById(roleId);
                return ResultData.success();
            }
            return new ResultData<>(RetCode.eModuleEmpty.getErrorcode(), RetCode.eModuleEmpty.getErrorMsg());
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}